//
//  BackgroundFetch.swift
//  pgy
//
//  Created by vsccw on 2018/12/28.
//  Copyright © 2018 vsccw.com. All rights reserved.
//

import UIKit

import RxSwift

class BackgroundFetch {
  
  private var disposeBag = DisposeBag()
  
  static let shared = BackgroundFetch()
  
  func setupBackgroundFetchTimeInterval(_ interval: TimeInterval) {
    UIApplication.shared.setMinimumBackgroundFetchInterval(interval)
  }
  
  func fetch(_ completion: @escaping (UIBackgroundFetchResult) -> Void) {
    API.shared.start()
      .subscribe(onNext: { (apps) in
        guard let app = apps.first else { return }
        LocalNotification.shared.triggerTitleNotification(app: app)
        completion(.newData)
      })
      .disposed(by: disposeBag)
  }
}
